﻿using FastCloud.Base;
using FastCloud.Base.DB;
using FastCloud.Base.Desgin;
using FastCloud.ORG.DB;
using FastCloud.ORG.Entity;
using FastIOC.Attribute;
using FastUtil.Convert;
using FastUtil.Data;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Controllers.ORG
{
    [Route("frame/org/user")]
    public class UserController : BaseController
    {
        [AutoWired]
        private DB_Config @DB_Config;
        [AutoWired]
        private DB_OU @DB_OU;
        [AutoWired]
        private DB_User @DB_User;
        [AutoWired]
        private DB_UserExtend @DB_UserExtend;

        [Route("list")]
        public IActionResult List()
        {
            return View("~/Views/ORG/User/User_List.cshtml");
        }

        [Route("add")]
        public IActionResult Add()
        {
            return View("~/Views/ORG/User/User_Add.cshtml");
        }

        [Route("edit")]
        public IActionResult Edit()
        {
            return View("~/Views/ORG/User/User_Edit.cshtml");
        }

        [Route("oplist"), HttpPost]
        public IActionResult OpList()
        {
            string UserName = ValueConvert.ConvertToString(GetFormParam("UserName"));
            string OUGuid = ValueConvert.ConvertToString(GetFormParam("OUGuid"));
            bool SearchInAll = ValueConvert.ConvertToBoolean(GetFormParam("SearchInAll"));
            string sql = "1=1";
            if (!string.IsNullOrEmpty(UserName))
            {
                sql += " and UserName like @UserName";
                DB_User.Add2Parameter("UserName", "%" + UserName + "%");
            }
            if (!SearchInAll)
            {
                if (!string.IsNullOrEmpty(OUGuid) && OUGuid!="Top")
                {
                    sql += " and OUGuid=@OUGuid";
                    DB_User.Add2Parameter("OUGuid", OUGuid);
                }
            }
            List<Frame_User> list = DB_User.SelectPageList("*", sql, "SortNum desc");
            List<Frame_OU> alloulist = DB_OU.SelectAllList("OUGuid,OUName,ParentOUGuid");
            foreach (Frame_User item in list)
            {
                item.OUName = DB_OU.GetFullPathOUName(item.OUGuid, alloulist);
            }
            int count = DB_User.SelectCount(sql);
            Page page = new Page();
            page.Rows = list;
            page.Total = count;
            return Result.OK(page);
        }

        [Route("opinsert"), HttpPost]
        public IActionResult OpInsert(Frame_User Entity)
        {
            Entity.Password = DB_Config.SelectConfigByName("DefaultPWD").ConfigValue;
            string UserGuid = string.IsNullOrEmpty(Entity.UserGuid) ? GenerateUtil.GenerateRowGuid() : Entity.UserGuid;
            Entity.UserGuid = UserGuid;
            DB_User.Insert(Entity);
            Frame_UserExtend ExtendEntity = new Frame_UserExtend();
            ExtendEntity.UserGuid = UserGuid;
            ExtendEntity.Birthday = Entity.Birthday;
            ExtendEntity.QQNumber = Entity.QQNumber;
            ExtendEntity.PostalAddress = Entity.PostalAddress;
            ExtendEntity.PostalCode = Entity.PostalCode;
            ExtendEntity.IdentityCardNum = Entity.IdentityCardNum;
            ExtendEntity.CarNum = Entity.CarNum;
            DB_UserExtend.Insert(ExtendEntity);
            return Result.OK();
        }

        [Route("opupdate"), HttpPost]
        public IActionResult OpUpdate(Frame_User Entity)
        {
            DB_User.UpdateByID(Entity);
            Frame_UserExtend ExtendEntity = new Frame_UserExtend();
            ExtendEntity.UserGuid = Entity.UserGuid;
            ExtendEntity.Birthday = Entity.Birthday;
            ExtendEntity.QQNumber = Entity.QQNumber;
            ExtendEntity.PostalAddress = Entity.PostalAddress;
            ExtendEntity.PostalCode = Entity.PostalCode;
            ExtendEntity.IdentityCardNum = Entity.IdentityCardNum;
            ExtendEntity.CarNum = Entity.CarNum;
            ExtendEntity.UpdateFieldList = Entity.UpdateFieldList;
            DB_UserExtend.UpdateByID(ExtendEntity);
            return Result.OK();
        }

        [Route("opdetail"), HttpPost]
        public IActionResult OpDetail()
        {
            string UserGuid = ValueConvert.ConvertToString(GetFormParam("UserGuid"));
            Frame_User Entity = DB_User.SelectOne(UserGuid);
            Frame_UserExtend ExtendEntity = DB_UserExtend.SelectOne(UserGuid);
            Entity.Birthday = ExtendEntity.Birthday;
            Entity.QQNumber = ExtendEntity.QQNumber;
            Entity.PostalAddress = ExtendEntity.PostalAddress;
            Entity.PostalCode = ExtendEntity.PostalCode;
            Entity.IdentityCardNum = ExtendEntity.IdentityCardNum;
            Entity.CarNum = ExtendEntity.CarNum;
            return Result.OK(Entity);
        }

        [Route("opbatchdelete"), HttpPost]
        public IActionResult OpBatchDelete()
        {
            string KeyID = ValueConvert.ConvertToString(GetFormParam("KeyID"));
            List<string> KeyIDList = KeyID.TrimEnd(';').Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            DB_User.DeleteBatch(KeyIDList);
            return Result.OK();
        }

        [Route("opinitpwd"), HttpPost]
        public IActionResult OpInitPassWord()
        {
            string UserGuid = ValueConvert.ConvertToString(GetFormParam("UserGuid"));
            DB_User.InitPWD(UserGuid);
            return Result.OK();
        }
    }
}
